<template>
  <XtxDialog v-model:visible="visibleDialog"
             title="修改收货地址">
    <!-- 表单 -->
    <template v-slot:body>
      <div class="address-edit">
        <div class="xtx-form">
          <div class="xtx-form-item">
            <label class="label" for="receiver">收货人: </label>
            <div class="field">
              <input id="receiver"
                     v-model="formData.receiver"
                     class="input"
                     placeholder="请输入收货人"
                     type="text">
            </div>
          </div>

          <div class="xtx-form-item">
            <label class="label" for="contact">手机号: </label>
            <div class="field">
              <input id="contact"
                     v-model="formData.contact"
                     class="input"
                     placeholder="请输入手机号"
                     type="number">
            </div>
          </div>

          <div class="xtx-form-item">
            <label class="label">地区: </label>
            <div class="field">
              <XtxCity :full-location="formData.fullLocation"
                       placeholder="请选择所在地区"
                       @change="changeCity"/>
            </div>
          </div>

          <div class="xtx-form-item">
            <label class="label" for="address">详细地址: </label>
            <div class="field">
              <input id="address"
                     v-model="formData.address"
                     class="input"
                     placeholder="请输入详细地址"
                     type="text">
            </div>
          </div>

          <div class="xtx-form-item">
            <label class="label" for="postalCode">邮政编码: </label>
            <div class="field">
              <input id="postalCode"
                     v-model="formData.postalCode"
                     class="input"
                     placeholder="请输入邮政编码"
                     type="number">
            </div>
          </div>

          <div class="xtx-form-item">
            <label class="label" for="addressTags">地址标签: </label>
            <div class="field">
              <input id="addressTags"
                     v-model="formData.addressTags"
                     class="input"
                     placeholder="请输入地址标签, 用逗号分隔"
                     type="text">
            </div>
          </div>
        </div>
      </div>
    </template>

    <!-- 按钮 -->
    <template v-slot:footer>
      <XtxButton style="margin-right: 20px" type="gray" @click="visibleDialog = false">取消</XtxButton>
      <XtxButton type="primary" @click="submit">确认</XtxButton>
    </template>
  </XtxDialog>
</template>

<script>

import API from '@/api/order'
import { reactive, ref } from 'vue'
import Message from '@/components/library/Message'

export default {
  name: 'AddressEdit',
  emits: ['on-success'],
  setup(props, { emit }) {
    const visibleDialog = ref(false)

    // 定义表单数据对象
    const formData = reactive({
      id: null,
      receiver: null,
      contact: null,
      provinceCode: null,
      cityCode: null,
      countyCode: null,
      address: null,
      postalCode: null,
      addressTags: null,
      isDefault: 0,       // 0表示是默认地址, 1为非默认
      // 城市组件显示文字 (完整行政区地址)
      fullLocation: null
    })

    // 定义一个open函数, 作为打开对话框函数(组件实例拥有open函数)
    // 父组件通过ref引用子组件的open函数
    const open = (address) => {
      if (address.id) {
        // 如果是修改, 填充表单
        Object.keys(address).forEach(key => formData[key] = address[key])
      }
      visibleDialog.value = true
    }

    // 城市选中
    const changeCity = (changeResult) => {
      formData.provinceCode = changeResult.provinceCode
      formData.cityCode = changeResult.cityCode
      formData.countyCode = changeResult.countyCode
      formData.fullLocation = changeResult.fullLocation
    }

    // 添加收货的提交操作 (修改也用这个)
    const submit = () => {
      // 1.省略了校验
      // 2.发送请求了
      API.editAddress(formData)
          .then(() => {
            Message({
              type: 'success',
              text: '修改收货地址成功'
            })
            // 关闭对话框
            visibleDialog.value = false
            emit('on-success', formData)    // 注意formData是reactive对象, 不用取value
            // 修改就不需要情况formData表单了
          })
          .catch((err) => {
            Message({
              text: `修改收货地址失败, ${err.response.data.message} `
            })
            visibleDialog.value = false
          })
    }

    return {
      visibleDialog,
      open,
      formData,
      changeCity,
      submit
    }
  }
}
</script>

<style lang="less" scoped>

.xtx-dialog {
  ::v-deep .wrapper {
    width: 780px;

    .body {
      font-size: 14px;
    }
  }
}

.xtx-form {
  margin: 0;

  input {
    outline: none;

    // 修改 input 输入框的默认值提示样式
    &::placeholder {
      color: #ccc;
    }
  }

  .xtx-form-item {

    .input {
      margin-left: 10px;
      height: 50px;
      line-height: 50px;

      text-indent: 10px;
      border: 1px solid #e4e4e4;
      border-radius: 3px;

      &:focus {
        border-color: @xtxColor;
      }
    }
  }
}

::v-deep .xtx-city {
  margin-left: 10px;
  width: 320px;

  .select {
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 50px;
    padding: 0 10px;

    .placeholder {
      color: #ccc;
    }

    i {
      font-size: 18px;
      color: #ccc;
    }

    .value {
      font-size: 14px;
    }
  }

  .option {
    top: 49px;

    min-height: 28px;
    line-height: 28px;
    padding: 5px 0 5px 10px;
  }
}
</style>
